package wwl.lsf.hellospringboot.test.多线程题;

import java.util.concurrent.CountDownLatch;
import lombok.AllArgsConstructor;

public class 按序打印 {

  public static void main(String[] args) {
    PrintMeg printMeg = new PrintMeg();
    Thread t1 = new Print(printMeg);
    Thread t2 = new Print2(printMeg);
    Thread t3 = new Print3(printMeg);

    t2.start();
    t1.start();
    t3.start();

  }


}

class PrintMeg {

  private CountDownLatch second=new CountDownLatch(1);
  private CountDownLatch third=new CountDownLatch(1);

  public void print() {
    System.out.println(1);
    second.countDown();
  }

  public void print2() throws InterruptedException {
    second.await();
    System.out.println(2);
    third.countDown();
  }

  public void print3() {
    try {
      third.await();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(3);
  }


}

@AllArgsConstructor
class Print extends Thread {


  private PrintMeg printMeg;

  @Override
  public void run() {
    printMeg.print();
  }
}

@AllArgsConstructor
class Print2 extends Thread {


  private PrintMeg printMeg;

  @Override
  public void run() {
    try {
      printMeg.print2();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

@AllArgsConstructor
class Print3 extends Thread {


  private PrintMeg printMeg;

  @Override
  public void run() {
    printMeg.print3();
  }
}